<?php
/**
 * 会员前台管理中心、账号管理、收藏操作类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class owners extends foreground {

	private $owners_db;
	
	function __construct() {
		parent::__construct();
		$this->http_user_agent = str_replace('7.0' ,'8.0',$_SERVER['HTTP_USER_AGENT']);
		
		$this->owners_db = pc_base::load_model('owners_model');
	
	}

	public function init() {
		$memberinfo = $this->memberinfo;
		//初始化phpsso
		$phpsso_api_url = $this->_init_phpsso();
		//获取头像数组
		$avatar = $this->client->ps_getavatar($this->memberinfo['phpssouid']);

		$grouplist = getcache('grouplist');
		$memberinfo['groupname'] = $grouplist[$memberinfo[groupid]]['name'];

		include template('member', 'index');
	}
	/*处理业主信息*/
	function owners_info(){
		$member = $this->owners_db->get_one(array('userid'=>$this->memberinfo['userid']));
		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['owners'];
			
			if(is_array($member)){
				$this->owners_db->update($data, array('userid'=>$member['userid']));
			}
			else
			{
				$data['userid']= $this->memberinfo['userid'];
				$this->owners_db->insert($data);
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
		else
		{
			include template('member', 'owners_info');
		}
	}
	function signed(){
		$memberinfo = $this->memberinfo;
		if($memberinfo['typeid']!=1){
			showmessage('你所以在用户组没有权限，请注册业主会员后发起签约', HTTP_REFERER);
		}
		$userid = $memberinfo['userid'];
		$this->signed_db = pc_base::load_model('signed_model');
		$table_pre = $this->signed_db->db_tablepre;
		
		$signed_db = $table_pre.'signed';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
		if($_GET['tn']==1){
				$member_designer_db = $table_pre.'member_designer';
			$sql = 'select a.*,b.name from '.$signed_db.' as a left join 
			'.$member_designer_db.' as b on a.c_userid=b.userid where a.usertypeid=4 and a.userid = '.$userid;
			$sql_count = 'select count(a.signedid ) as num from '.$signed_db.' as a left join 
			'.$member_designer_db.' as b on a.c_userid=b.userid where a.usertypeid=4 and a.userid = '.$userid;
			
		}else{
			
			$member_company_db = $table_pre.'member_company';
			$sql = 'select a.*,b.companyname from '.$signed_db.' as a left join 
			'.$member_company_db.' as b on a.c_userid=b.userid where a.usertypeid=2 and a.userid = '.$userid;
			$sql_count = 'select count(a.signedid ) as num from '.$signed_db.' as a left join 
			'.$member_company_db.' as b on a.c_userid=b.userid   where a.usertypeid=2 and a.userid = '.$userid;
		}
		
		
		$sql .= " LIMIT $offset,$pagesize";
//	echo $sql;
		$this->signed_db->query($sql);
		$signed_data = $this->signed_db->fetch_array();
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		//echo '<pre>';
		//print_r($signed_data);exit;
		$total = $total[0]['num'];
		
		$pages = pages($total, $page, $pagesize);
		include template('member', 'owners_signed');
	}
	//业主对公司的评价页面
	function appraisal(){
		$this->signed_db = pc_base::load_model('signed_model');
		if($_POST['dosubmit']){
			$data = $_POST['pj'];
			$data['inputtime'] = strtotime("now");
			$signedid = intval($_POST['signedid']);
			if($signedid<1) return '';
			$result = $this->signed_db->update($data, 'signedid='.$signedid);
			$msg = $result==1?'评价成功':'评价失败，请重试';
			showmessage($msg, HTTP_REFERER);
		}else{
			$signedid = intval($_GET['signedid']);
			if($signedid<1) return '';
			$this->company_db = pc_base::load_model('company_model');
			
			$signing_data = $this->signed_db->get_one('signedid='.$signedid);
			$company_data = $this->company_db->get_one('userid='.$signing_data['c_userid']);
			//print_r($company_data);
			include template('member', 'owners_appraisal');
		}
	}
//评价列表
	function appraisal_list(){
		$this->signed_db = pc_base::load_model('signed_model');
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 12;
		$offset = intval($pagesize*($page-1));
		$signedid = intval($_GET['signedid']);
		if($signedid){$con = ' and signedid='.$signedid;}
		$sql = 'SELECT *  FROM '.$this->signed_db->table_name.' WHERE userid='.$this->memberinfo['userid'].$con.' order by signedid desc ';
		$sql_count = 'select count(signedid) as num FROM '.$this->signed_db->table_name.' WHERE userid='.$this->memberinfo['userid'].$con.' order by signedid desc ';
		
		$sql .= " LIMIT $offset,$pagesize ";
		
		$this->signed_db->query($sql);
		$pj_data = $this->signed_db->fetch_array();
		//print_r($pj_data);exit;
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		$total = $total[0]['num'];
		$pagesbar = pages($total, $page, $pagesize);
		include template('member', 'owners_appraisal_list');
	}
	
}
?>